﻿using Hangfire;
using Hangfire.Redis.StackExchange;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

public static class BackgroundJobExtensions
{
    public static void AddHangfireSetup(this IServiceCollection services, IConfiguration configuration)
    {
        // Add Hangfire services.
        services.AddHangfire(config => config
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseRedisStorage(configuration["Redis:ConnectionString"],
                new RedisStorageOptions
                {
                    Prefix = "hangfrie:",
                })
        );

        // Add the processing server as IHostedService
        services.AddHangfireServer();
    }
}